package com.lanou3g.ptp.warning.mapper;

import com.lanou3g.ptp.entity.WarningPlan;
import com.lanou3g.ptp.vo.Option;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface WarningPlanMapper {
    @Select("SELECT * FROM warning_plan WHERE type=#{type} AND status=0 ORDER BY update_time DESC")
    List<WarningPlan> findByType(Integer type);

    @Insert("INSERT INTO warning_plan(`name`,type,times_in_a_row,times_per_week,times_in_total,create_time," +
            "update_time,status) " +
            "VALUES(#{name},#{type},#{timesInARow},#{timesPerWeek},#{timesInTotal},#{createTime},#{updateTime}," +
            "#{status})")
    int insert(WarningPlan warningPlan);

    @Delete("DELETE FROM warning_plan WHERE id=#{warningPlanId}")
    int deleteById(Long warningPlanId);

    int updateById(WarningPlan warningPlan);

    @Select("SELECT * FROM warning_plan WHERE id=#{warningPlanId} AND status=0")
    WarningPlan findById(Long warningPlanId);

    @Select("SELECT id, `name` FROM warning_plan WHERE type=#{type} AND status=0 ORDER BY update_time DESC")
    List<Option> options(Integer type);

    @Select("SELECT wp.* FROM task_warning_plan twp LEFT JOIN warning_plan wp ON twp.warning_plan_id=wp.id " +
            "WHERE twp.task_id=#{taskId} AND wp.status=0 ORDER BY wp.type")
    List<WarningPlan> findByTaskId(Long taskId);
}
